package org.com.ashen.web.interceptor;

import org.com.ashen.service.config.JdbcConfig;
import org.com.ashen.service.dao.SaasAppSessionDao;
import org.com.ashen.service.datasourceswitch.DataSourceChange;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * Created by yizhenn on 16-12-10.
 */
public class DefaultInterceptor extends HandlerInterceptorAdapter {

    private List<AbstractInterceptor> abstractInterceptorList;
    private List<String> exceptUrls;  
    
    public List<String> getExceptUrls() {  
        return exceptUrls;  
    }  
    
    public void setExceptUrls(List<String> exceptUrls) {  
        this.exceptUrls = exceptUrls;  
    }  
    @Resource(name = "saasAppSessionDao")
	private SaasAppSessionDao saasAppSessionDao;

	@Resource(name = "jdbcConfig")
	private JdbcConfig jdbcConfig;
	
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        for(AbstractInterceptor abstractInterceptor:abstractInterceptorList){
//            if(!abstractInterceptor.check(request,response)){
//                return false;
//            }
//        }
//        return super.preHandle(request, response, handler);
//    }
    
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String sessionid = request.getParameter("sessionid");
//        DataSourceChange.setDataSource(saasAppSessionDao, sessionid, jdbcConfig);
		
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //做一些统计工作
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

//    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        super.afterConcurrentHandlingStarted(request, response, handler);
//    }

    public void setAbstractInterceptorList(List<AbstractInterceptor> abstractInterceptorList) {
        this.abstractInterceptorList = abstractInterceptorList;
    }
}
